<?php

// ----------------------------------------------------------------------
// zCart Shopping Cart Module for Zikula
// Based on pnCommerce 0.99
// ----------------------------------------------------------------------
// Source file:  $Source: W:\dev\pninit.php $
//
// Last changes from:  $Author: Floxxx $
//
// Revision:           $Revision: 0.1 $
//
// Date of revision    $Date: 6/17/2009 6:22:49 AM $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------
// Purpose of this file: Displaying a simple shoppingcart on your website
//      Creates the tables, starts the cart config
// ----------------------------------------------------------------------

function zCart_init()
{

    if (!DBUtil::createTable('zcart_items')) {
        return false;
    }

    // Create the index
    if (!DBUtil::createIndex('idx_items_sku', 'zcart_items', 'item_sku') ||
        !DBUtil::createIndex('idx_items_name', 'zcart_items', 'item_name')){
        return false;
    }

    //Create other tables
    zCart_createTables_101 ();

    // Set up config variables
    pnModSetVar ('zCart', 'ShopIntroTemplateID', 1);
    pnModSetVar ('zCart', 'DefaultCategoryViewTemplateID', 2);
    pnModSetVar ('zCart', 'DefaultItemViewTemplateID', 4);
    pnModSetVar ('zCart', 'MailTemplateID', 5);
    pnModSetVar ('zCart', 'InternalMailTemplateID', 9);
    pnModSetVar ('zCart', 'InvoiceTemplateID', 10);
    pnModSetVar ('zCart', 'PrintInvoiceTemplateID', 11);

    pnModSetVar ('zCart', 'MaximumCartLifetime', 120);

    pnModSetVar ('zCart', 'TNCreateMethod', 'gd' );

    pnModSetVar ('zCart', 'VATID1', 0 );
    pnModSetVar ('zCart', 'VATID2', 0 );
    pnModSetVar ('zCart', 'VATID3', 0 );
    pnModSetVar ('zCart', 'VATID4', 0 );
    pnModSetVar ('zCart', 'VATID5', 0 );
    pnModSetVar ('zCart', 'VATID6', 0 );
    pnModSetVar ('zCart', 'VATID7', 0 );
    pnModSetVar ('zCart', 'VATID8', 0 );
    pnModSetVar ('zCart', 'VATID9', 0 );

    pnModSetVar ('zCart', 'WeightUnit', "_PNC_KG" );
    pnModSetVar ('zCart', 'ShowItems', 10 );
    pnModSetVar ('zCart', 'Version', "0.1" );
    pnModSetVar ('zCart', 'MonetarySetting', pnConfigGetVar('locale') );
    pnModSetVar ('zCart', 'IntlCurrencySymbol', '' );
    pnModSetVar ('zCart', 'AddressFormat', _PNC_DEFAULTADDRESSFORMAT );
    pnModSetVar ('zCart', 'DateFormat', _PNC_DEFAULTDATEFORMAT );
    pnModSetVar ('zCart', 'TimeFormat', _PNC_DEFAULTTIMEFORMAT );

    pnModSetVar ('zCart', 'status', 'off' );
    pnModSetVar ('zCart', 'MaximumLogLevel', _PNC_LOG_NOTICE );
    pnModSetVar ('zcart', 'UseAuthKeys', 'on' );

    $emptyarray = array();
    pnModSetVar( 'zCart', 'ItemModifierSequence', serialize( $emptyarray ) );
    pnModSetVar( 'zCart', 'OrderModifierSequence', serialize( $emptyarray ) );
    pnModSetVar( 'zCart', 'PreOrderModifierSequence', serialize( $emptyarray ) );
    pnModSetVar( 'zCart', 'PostOrderModifierSequence', serialize( $emptyarray ) );

    // register hooks
    if (!pnModRegisterHook('item', 'display', 'GUI', 'zCart', 'user', 'view'))
    {
    	return LogUtil::registerError(_ERRORCREATINGHOOK);
    }

    // Initialisation successful
    return true;
}
/**
 * create tables
*/
function zCart_createTables_101 ()
{
	if (!DBUtil::createTable('zcart_cartitems')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_cartitems_userid', 'zcart_cartitems', 'user_id')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_categories')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_categories_parent', 'zcart_categories', 'category_parent')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_invoices')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_invoice_userid', 'zcart_invoices', 'user_id')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_logs')) {
	    return false;
	}

        if (!DBUtil::createTable('zcart_orders')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_order_userid', 'zcart_orders', 'user_id')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_properties')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_scheme_id', 'zcart_properties', 'scheme_id')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_propertiesdata')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_property_id', 'zcart_propertiesdata', 'property_id')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_schemes')) {
	    return false;
	}

	if (!DBUtil::createIndex('idx_template_id', 'zcart_schemes', 'template_id')) {
	    return false;
	}

	if (!DBUtil::createTable('zcart_suppliers')) {
	    return false;
	}

        if (!DBUtil::createTable('zcart_templates')) {
	    return false;
	}

	if (!zCart_populate_templates()) {
	    return false;
	}

        if (!DBUtil::createTable('zcart_users')) {
	    return false;
	}


	return true;

}

function zCart_populate_templates ()
{

	$templates = array();
	$template["template_id"] = 1;
        $template["template_type"] = 1;
        $template["template_file"] = "zCart_user_main.html";
        $template["template_description"] = "DefaultShopIntroTemplate";

        $templates[] = $template;

	$template["template_id"] = 2;
        $template["template_type"] = 2;
        $template["template_file"] = "zCart_user_categoryview.html";
        $template["template_description"] = "DefaultCategoryViewTemplate";

        $templates[] = $template;

	$template["template_id"] = 4;
        $template["template_type"] = 4;
        $template["template_file"] = "zCart_user_itemview.html";
        $template["template_description"] = "DefaultItemViewTemplate";

        $templates[] = $template;

	$template["template_id"] = 5;
        $template["template_type"] = 5;
        $template["template_file"] = "zCart_email_user.html";
        $template["template_description"] = "DefaultEmailTemplate";

        $templates[] = $template;

	$template["template_id"] = 9;
        $template["template_type"] = 5;
        $template["template_file"] = "zCart_email_owner.html";
        $template["template_description"] = "DefaultInternalEmailTemplate";

        $templates[] = $template;

	$template["template_id"] = 10;
        $template["template_type"] = 9;
        $template["template_file"] = "zCart_invoice_display.html";
        $template["template_description"] = "DefaultInvoiceTemplate";

        $templates[] = $template;

	$template["template_id"] = 11;
        $template["template_type"] = 9;
        $template["template_file"] = "zCart_invoice_print.html";
        $template["template_description"] = "DefaultPrintInvoiceTemplate";

        $templates[] = $template;

	if (!DBUtil::insertObjectArray($templates, "zcart_templates"))
	{
		return false;
	}
	return true;

}


function zCart_upgrade($oldversion)
{
// No upgrades yet?
/*    if (!DBUtil::changeTable('ephem')) {
        return false;
    }

    // Upgrade dependent on old version number
    switch($oldversion) {
        case 1.2:
            // version 1.2 shipped with postnuke .72x/.75
            pnModSetVar('Ephemerids', 'itemsperpage', 25);
            break;
    }

    // upgrade success */
    return true;
}

function zCart_delete()
{
    if (!DBUtil::dropTable('zcart_users')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_templates')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_suppliers')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_schemes')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_propertiesdata')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_properties')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_orders')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_logs')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_invoices')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_categories')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_cartitems')) {
        return false;
    }

    if (!DBUtil::dropTable('zcart_items')) {
        return false;
    }

    // Delete module variables
    pnModDelVar('zCart');

    // Deletion successful
    return true;
}


?>